package br.com.skytef.beans.WEB;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import br.com.skytef.dao.OrderDao;
import br.com.skytef.entity.TotalPedidosEntity;
import br.com.skytef.entity.TotalProdutosVendidosEntity;
import br.com.skytef.utils.Dates;

@ManagedBean(name = "TotalPedidosBean")
@RequestScoped
public class TotalPedidosBean {

	private List<TotalPedidosEntity> dataMaster;
	private List<TotalPedidosEntity> dataMaster2;
	private List<TotalProdutosVendidosEntity> dataDetail;
	private Date dataInicial;
	private Date dataFinal;
	private boolean group;
	
	public TotalPedidosBean() throws IOException {
		super();
	}

	@PostConstruct
	public void init() throws Exception {
		
		dataInicial = Dates.getDateNow();
		dataFinal = Dates.getDateNow();
		group = true;
		loadAllData();
		loadPageReport();

	}
	
	public void loadAllData() throws SQLException, Exception{

		if (dataInicial==null) {
			dataInicial = Dates.getDateNow();
		}
		
		if (dataFinal==null) {
			dataFinal = dataInicial;
		}
		
		if (Dates.isMaxDate(dataInicial, dataFinal)) {
			FacesContext facesContext = FacesContext.getCurrentInstance(); 
			facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erro:", "A data inicial deve ser menor ou igual a data final."));
		}else{
			this.loadReportMaster(dataInicial,dataFinal);
			this.loadReportDetail(group, dataInicial, dataFinal);			
		}
	}

	public void loadReportMaster(Date dateIni, Date dateFin) throws SQLException, Exception {
		dataMaster = new OrderDao().buscarTotais(dateIni, dateFin);
	}

	public void loadReportDetail(boolean grp,Date dateIni, Date dateFin) throws SQLException, Exception {
		dataDetail = new OrderDao().buscarProdVendidos(grp, dateIni, dateFin);
	}
	
	public void loadPageReport() throws Exception{
		Date dateIni = new Date();
		Date dateFim = new Date();
		dataMaster2 = new OrderDao().buscarTotais(dateIni, dateFim);
	}

	/*-----------------------------------------------------------------------------------------------
	 * 
	 -----------------------------------------------------------------------------------------------*/
	
	public List<TotalPedidosEntity> getDataMaster() {
		return dataMaster;
	}

	public void setDataMaster(List<TotalPedidosEntity> dataMaster) {
		this.dataMaster = dataMaster;
	}

	public List<TotalProdutosVendidosEntity> getDataDetail() {
		return dataDetail;
	}

	public void setDataDetail(List<TotalProdutosVendidosEntity> dataDetail) {
		this.dataDetail = dataDetail;
	}

	public Date getDataInicial() {
		return dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	public Date getDataFinal() {
		return dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}

	public boolean isGroup() {
		return group;
	}

	public void setGroup(boolean group) {
		this.group = group;
	}

	public List<TotalPedidosEntity> getDataMaster2() {
		return dataMaster2;
	}

	public void setDataMaster2(List<TotalPedidosEntity> dataMaster2) {
		this.dataMaster2 = dataMaster2;
	}
}
